<script lang="ts" setup>
import type { AiFormTemplatesApi } from '#/api/ai/form/formTemplates';

import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { getFormTemplates } from '#/api/ai/form/formTemplates';
import { useDescription } from '#/components/description';
import DynamicForm from '#/components/dynamic-form/index.vue';

import { useDetailSchema } from '../data';
import { Tabs } from 'ant-design-vue';
import { getFormVariablesList } from '#/api/ai/form/formVariables';
const activeKey = ref('afterSale');
const formData = ref<AiFormTemplatesApi.FormTemplates>();
const variablesList = ref([]);
const [Descriptions] = useDescription({
  bordered: true,
  size: 'middle',
  column: 2,
  labelWidth: 120,

  schema: useDetailSchema(),
});

const handleSubmit = () => {};
function handleTabChange(key: any) {
  activeKey.value = key;
}
const [Modal, modalApi] = useVbenModal({
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    let data = modalApi.getData<AiFormTemplatesApi.FormTemplates>();
    if (!data || !data.id) {
      return;
    }
    modalApi.lock();
    try {
      data = await getFormTemplates(data.id);
      formData.value = data;

      variablesList.value = await getFormVariablesList({ formId: data.id });
    } finally {
      modalApi.unlock();
    }
  },
});
</script>

<template>
  <Modal
    title=""
    class="w-[60%]"
    :show-cancel-button="false"
    :show-confirm-button="false"
  >
    <Tabs :active-key="activeKey" @change="handleTabChange">
      <Tabs.TabPane tab="基本信息" key="afterSale" :force-render="true" />
      <Tabs.TabPane tab="表单查看" key="delivery" :force-render="true" />
    </Tabs>
    <Descriptions
      v-if="activeKey === 'afterSale'"
      :data="formData"
      class="mx-4"
    />

    <DynamicForm
      v-if="activeKey === 'delivery'"
      :list="variablesList"
      @submit="handleSubmit"
    />
  </Modal>
</template>
